package com.bluse.config;


import com.bluse.exception.MyErrorException;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Describe: TODO
 * @Author: bluse
 * @Date: 2018/12/25 18:15
 **/

@Configuration
public class RestSecurityConfig extends WebMvcConfigurationSupport {

    /**
     * Rest Request  Interceptors
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new HandlerInterceptor() {
            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                    throws Exception {
                String appkey = request.getHeader("appkey");
                if (appkey == null) {
//                    throw new MyErrorException("2023", "appkey未配置");
                    return true;
                }
                return true;
            }
        }).excludePathPatterns("/apidoc/**")
                .excludePathPatterns("/login")
                .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**")

        ;
    }

    /**
     * resoruceHandler registry
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/apidoc/**").addResourceLocations("classpath:/api-doc/");
    }
}
